package com.ghck.server.controller;

import java.security.Principal;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ghck.server.controller.access.ArgIndexForPrintLog;
import com.ghck.server.exception.GhckException;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.model.NurseStationDict;
import com.ghck.server.model.query.NurseStationDictQuery;
import com.ghck.server.model.response.ResultList;
import com.ghck.server.service.NurseStationDictService;
import com.ghck.server.service.RegionDictService;
import com.ghck.server.utils.IntegerUtil;

@RequestMapping("/nurse_station")
@RestController
public class NurseStationController {
	
	Logger logger = LoggerFactory.getLogger(NurseStationController.class);
	
	@Autowired
	NurseStationDictService nurseStationDictService;

	@Autowired
	RegionDictService regionDictService;

	/**
	 * regionId=0表示拉取所有
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/fetch/{regionId}", method = RequestMethod.GET)
	public ResultList<NurseStationDict> fetchAllDepartment(@PathVariable("regionId") Integer regionId,Integer startId,Integer endId,Integer start,Integer limit,Principal principal){
		long startTime = System.currentTimeMillis();
 		long _startTime = startTime;
 		long endTime =0L;
 		
		if(logger.isDebugEnabled()){
 			logger.debug("======== 拉取护士站 start ========");
 		}
		NurseStationDictQuery query =  new NurseStationDictQuery();
		query.setStartId(startId);
		query.setStart(start);
		query.setEndId(endId);
		query.setLimit(limit);
		if(IntegerUtil.isPositiveInteger(regionId)){
			List<Integer> regionIds = regionDictService.findSelfAllChildrenId(regionId);
			
			if(logger.isDebugEnabled()){
	 			endTime = System.currentTimeMillis();
	 			logger.debug("regionDictService.findSelfAllChildrenId time:{}ms",(endTime-startTime));
	 			startTime = endTime;
	 		}
			
			if (CollectionUtils.isNotEmpty(regionIds)) {
				query.setRegionIds(regionIds);
			}
		}
		List<NurseStationDict> nurseStations = nurseStationDictService.findNurseStationDictByQuery(query);
		if(logger.isDebugEnabled()){
 			endTime = System.currentTimeMillis();
 			logger.debug("nurseStationDictService.findNurseStationDictByQuery time:{}ms",(endTime-startTime));
 			startTime = endTime;
 			
 			logger.debug("======== 拉取护士站  end,总time:{}ms ========",(endTime-_startTime));
 		}
		return new ResultList<>(nurseStations);
	}
	
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/fetch_by_query", method = RequestMethod.GET)
	public ResultList<NurseStationDict> fetchAllDepartment(NurseStationDictQuery query){
		long startTime = System.currentTimeMillis();
 		long _startTime = startTime;
 		long endTime =0L;
 		
		if(logger.isDebugEnabled()){
 			logger.debug("======== 拉取护士站 start ========");
 		}

		Integer regionId = query.getRegionId();
		if(IntegerUtil.isPositiveInteger(regionId)){
			List<Integer> regionIds = regionDictService.findSelfAllChildrenId(regionId);
			
			if(logger.isDebugEnabled()){
	 			endTime = System.currentTimeMillis();
	 			logger.debug("regionDictService.findSelfAllChildrenId time:{}ms",(endTime-startTime));
	 			startTime = endTime;
	 		}
			
			if (CollectionUtils.isNotEmpty(regionIds)) {
				query.setRegionIds(regionIds);
			}
		}
		List<NurseStationDict> nurseStations = nurseStationDictService.findNurseStationDictByQuery(query);
		if(logger.isDebugEnabled()){
 			endTime = System.currentTimeMillis();
 			logger.debug("nurseStationDictService.findNurseStationDictByQuery time:{}ms",(endTime-startTime));
 			startTime = endTime;
 			
 			logger.debug("======== 拉取护士站  end,总time:{}ms ========",(endTime-_startTime));
 		}
		return new ResultList<>(nurseStations);
	}
	
	/**
	 * 通过 
	 * 经度
	 * longitude
	 * 纬度
	 * latitude
	 * 查询护士站
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/fetch_by_location", method = RequestMethod.GET)
	public ResultList<NurseStationDict> findByLongitudeAndLatitude(NurseStationDictQuery query){
		if(query.getLongitude()==null || query.getLongitude()<=0){
			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"经度不能为空");
			logger.info("查询失败",exception);
			throw exception;
		}
		if(query.getLatitude()==null || query.getLatitude()<=0){
			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"纬度不能为空");
			logger.info("查询失败",exception);
			throw exception;
		}
		
		List<NurseStationDict> nurseStations = nurseStationDictService.findNurseStationDictOrderByDistince(query);
		
		return new ResultList<>(nurseStations);
		
	}
	
//	/**
//	 * 添加护士站
//	 */
//	@ArgIndexForPrintLog(indexes={0})
//	@RequestMapping(value = "/add", method = RequestMethod.POST)
//	public ResultObject<Integer> addNurseStationDict(@RequestBody NurseStationDict dict){
//		if(dict.getLongitude()==null || dict.getLongitude()<=0){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"经度不能为空");
//			logger.info("添加护士站失败",exception);
//			throw exception;
//		}
//		if(dict.getLatitude()==null || dict.getLatitude()<=0){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"纬度不能为空");
//			logger.info("添加护士站失败",exception);
//			throw exception;
//		}
//		Integer id = nurseStationDictService.insertNurseStationDict(dict);
//		
//		return new ResultObject<>(id);
//	}
//	
//	/**
//	 * 修改护士站
//	 */
//	@ArgIndexForPrintLog(indexes={0})
//	@RequestMapping(value = "/modify", method = RequestMethod.PUT)
//	public ResultObject<Integer> modifyNurseStationDict(@RequestBody NurseStationDict dict){
//		if(dict.getLongitude()==null || dict.getLongitude()<=0){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"id不能为空");
//			logger.info("修改护士站失败",exception);
//			throw exception;
//		}
//		
//		Integer id = nurseStationDictService.updateNurseStationDict(dict);
//		
//		return new ResultObject<>(id);
//	}
	
}
